﻿/*
 * Copyright (c) HiSilicon (Shanghai) Technologies Co., Ltd. 2023-2023. All rights reserved.
 */

#include "ChangeChip.h"
#include <QSettings>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QLabel>
#include <QPushButton>
#include "BurnToolCommon/Global.h"

namespace BurnDlg {

namespace {
    const int WINDOW_WIDTH = 240;
    const int WINDOW_HEIGHT = 110;
    const int BUTTON_OK = 0;
    const int BUTTON_CANCEL = 1;
    const int OK_SCALE = 2;
    const int CANCEL_SCALE = 1;
}

ChangeChip::ChangeChip(QWidget *parent)
    : QWidget(parent),
      comboBox(nullptr)
{
    InitUi();
    comboBox->setCurrentText(Global::GetCurrentChipVer());
}

void ChangeChip::InitUi()
{
    setFixedSize(WINDOW_WIDTH, WINDOW_HEIGHT);
    setWindowTitle("Burn Tools");
    setWindowFlags(Qt::Dialog | Qt::WindowCloseButtonHint);
    setAttribute(Qt::WA_DeleteOnClose);

    QVBoxLayout* v1 = new QVBoxLayout();
    QHBoxLayout* h1 = new QHBoxLayout();
    QHBoxLayout* h2 = new QHBoxLayout();
    QLabel* label1 = new QLabel(tr("Chip List"));
    comboBox = new MyComboBox();
    QStringList chipTypeList;
    chipTypeList.clear();
    for (auto it = Global::allChipTypeInfo.begin(); it != Global::allChipTypeInfo.end(); ++it) {
        chipTypeList.push_back(it.value().name);
    }
    chipTypeList.sort();
    comboBox->addItems(chipTypeList);
    comboBox->setCurrentText(Global::ChipTypeIdToString(Global::currentChipTypeId));
    h1->addStretch();
    h1->addWidget(label1);
    h1->addWidget(comboBox);
    h1->addStretch();
    QPushButton* ok = new QPushButton(tr("OK"));
    QPushButton* cancel = new QPushButton(tr("Cancel"));
    h2->addStretch();
    h2->addWidget(ok);
    h2->addWidget(cancel);
    h2->addStretch();
    v1->addLayout(h1);
    v1->addLayout(h2);
    v1->setStretch(BUTTON_OK, OK_SCALE);
    v1->setStretch(BUTTON_CANCEL, CANCEL_SCALE);
    setLayout(v1);
    connect(ok, SIGNAL(clicked()), this, SLOT(SlotOKClick()));
    connect(cancel, SIGNAL(clicked()), this, SLOT(SlotCancelClick()));
}

void ChangeChip::SlotOKClick()
{
    if (comboBox->currentText() == Global::ChipTypeIdToString(Global::currentChipTypeId)) {
        this->close();
        return;
    }
    emit SignalChanceChipVersion(comboBox->currentText());
    this->close();
}

void ChangeChip::SlotCancelClick()
{
    this->close();
}

}